package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ObjectAttribute;
import com.ruoyi.system.service.IObjectAttributeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 属性Controller
 * 
 * @author ruoyi
 * @date 2020-03-04
 */
@Controller
@RequestMapping("/system/bute")
public class ObjectAttributeController extends BaseController
{
    private String prefix = "system/bute";

    @Autowired
    private IObjectAttributeService objectAttributeService;

    @RequiresPermissions("system:bute:view")
    @GetMapping()
    public String bute()
    {
        return prefix + "/bute";
    }

    /**
     * 查询属性树列表
     */
    @RequiresPermissions("system:bute:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ObjectAttribute> list(ObjectAttribute objectAttribute)
    {
        List<ObjectAttribute> list = objectAttributeService.selectObjectAttributeList(objectAttribute);
        return list;
    }

    /**
     * 导出属性列表
     */
    @RequiresPermissions("system:bute:export")
    @Log(title = "属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ObjectAttribute objectAttribute)
    {
        List<ObjectAttribute> list = objectAttributeService.selectObjectAttributeList(objectAttribute);
        ExcelUtil<ObjectAttribute> util = new ExcelUtil<ObjectAttribute>(ObjectAttribute.class);
        return util.exportExcel(list, "bute");
    }

    /**
     * 新增属性
     */
    @GetMapping(value = { "/add/{objectId}", "/add/" })
    public String add(@PathVariable(value = "objectId", required = false) Long objectId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(objectId))
        {
            mmap.put("objectAttribute", objectAttributeService.selectObjectAttributeById(objectId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存属性
     */
    @RequiresPermissions("system:bute:add")
    @Log(title = "属性", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ObjectAttribute objectAttribute)
    {
        return toAjax(objectAttributeService.insertObjectAttribute(objectAttribute));
    }

    /**
     * 修改属性
     */
    @GetMapping("/edit/{objectId}")
    public String edit(@PathVariable("objectId") Long objectId, ModelMap mmap)
    {
        ObjectAttribute objectAttribute = objectAttributeService.selectObjectAttributeById(objectId);
        mmap.put("objectAttribute", objectAttribute);
        return prefix + "/edit";
    }

    /**
     * 修改保存属性
     */
    @RequiresPermissions("system:bute:edit")
    @Log(title = "属性", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ObjectAttribute objectAttribute)
    {
        return toAjax(objectAttributeService.updateObjectAttribute(objectAttribute));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:bute:remove")
    @Log(title = "属性", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{objectId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("objectId") Long objectId)
    {
        return toAjax(objectAttributeService.deleteObjectAttributeById(objectId));
    }

    /**
     * 选择属性树
     */
    @GetMapping(value = { "/selectButeTree/{objectId}", "/selectButeTree/" })
    public String selectButeTree(@PathVariable(value = "objectId", required = false) Long objectId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(objectId))
        {
            mmap.put("objectAttribute", objectAttributeService.selectObjectAttributeById(objectId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载属性树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = objectAttributeService.selectObjectAttributeTree();
        return ztrees;
    }
}
